package com.jerei.developer.service.impl;

import com.jerei.developer.service.IMasterSlaveTableGenerateService;
import com.jerei.developer.utils.UtilsRoot;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by sunfeihu on 2016/10/19.
 * 主从表生成工具
 */
public class MySqlMasterSlaveTableGenerateServiceImpl implements IMasterSlaveTableGenerateService {

    @Autowired
    private UtilsRoot utilsRoot;

    private String pluginId;

    @Override
    public void gEntity(String masterTableName, String slaveTableName) {
        //先生成主表
        utilsRoot.templateOut("/templates/Entity.java", "", masterTableName,"",this.pluginId);
        //再生成从表
        utilsRoot.templateOut("/templates/Entity.java", "", slaveTableName,"",this.pluginId);
    }


    /**
     * 通用生成，用于生成Service和Dao
     *
     * @param tableName 表面
     * @param layerName 三层的层名，Service和Dao
     */
    private void gCommon(String tableName, String layerName) {
        utilsRoot.templateOut("/templates/" + layerName + "Impl.java", "Impl", tableName,"",this.pluginId);
        utilsRoot.templateOut("/templates/" + layerName + "Impl.java", layerName, tableName,"",this.pluginId);
    }

    @Override
    public void gControl(String masterTableName, String slaveTableName) {
        utilsRoot.templateOut("/templates/Control.java", "Control", masterTableName,"",this.pluginId);
        utilsRoot.templateOut("/templates/Control.java", "Control", slaveTableName,"",this.pluginId);
    }

    @Override
    public void gService(String masterTableName, String slaveTableName) {
        this.gCommon(masterTableName, "Service");
        this.gCommon(slaveTableName, "Service");
    }

    @Override
    public void gDao(String masterTableName, String slaveTableName) {
        this.gCommon(masterTableName, "Dao");
        this.gCommon(slaveTableName, "Dao");
    }

    @Override
    public void gList(String masterTableName, String slaveTableName) {

    }

    @Override
    public void gEdit(String masterTableName, String slaveTableName) {

    }

    @Override
    public void gView(String masterTableName, String slaveTableName) {

    }

    @Override
    public void gAll(String masterTableName, String slaveTableName) {
        this.gEntity(masterTableName, slaveTableName);
        this.gDao(masterTableName, slaveTableName);
        this.gService(masterTableName, slaveTableName);
        this.gControl(masterTableName, slaveTableName);
        this.gEdit(masterTableName, slaveTableName);
        this.gList(masterTableName, slaveTableName);
    }
}